package com.beiding.pojo.room;

import com.beiding.utils.ValidateUtils;
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Lists;
import com.google.common.collect.Sets;
import org.springframework.binding.message.MessageContext;
import org.springframework.binding.validation.ValidationContext;
import org.springframework.stereotype.Component;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

@Component
public class RoomValidator {


    public void validateBasic(Room room, ValidationContext context) {


        MessageContext messageContext = context.getMessageContext();
        if (room.getName().length() > 10) {
            messageContext.addMessage(ValidateUtils.error("name", "不能多于十个字符"));
        } else if (room.getName().length() < 2) {
            messageContext.addMessage(ValidateUtils.error("name", "不能少于两个字符"));
        }

        if (room.getDescription().length() > 140) {
            messageContext.addMessage(ValidateUtils.error("description", "不能多于140个字符"));
        } else if (room.getDescription().length() < 10) {
            messageContext.addMessage(ValidateUtils.error("description", "不能少于10个字符"));
        }


        //处理图片Id
        Set<String> images = room.getImages();

        if (images != null) {
            HashSet<String> copy = Sets.newHashSet(images);
            copy.forEach(s -> {

                //如果图片id使用d-作为前缀,则表明需要被删除
                if (s.startsWith("d-")) {
                    String id = s.substring(2);
                    String ap = "a-" + id;

                    //如果images中同时包含d-前缀以及a-前缀的同一个id.那么这两个要被删除.(中和)
                    if (images.contains(ap) && images.contains(s)) {
                        images.remove(ap);
                        images.remove(s);
                    } else if (!s.contains(id)) {//如果在原图片数组中不包含该图片,则该移除选项无效
                        images.remove(s);
                    } else {//如果原图片中包含该图片
                        //使用该d-作为前缀的替换掉原id
                        images.remove(id);
                    }
                } else if (s.startsWith("a-")) {//如果使用a-作为前缀
                    if (images.contains(s.substring(2))) {//如果原图片里面包含该添加项,那么认为该添加项无效
                        images.remove(s);
                    }
                }
            });
        }

    }


    public void validateLocation(Room room, ValidationContext context) {


        MessageContext messageContext = context.getMessageContext();
        Location location = room.getLocation();

        //获取房间的坐标

        if (!(-180 <= location.getLongitude() && location.getLongitude() < 180 && -80 <= location.getLatitude() && location.getLatitude() <= 80)) {
            messageContext.addMessage(ValidateUtils.error("location.detailedDescription", "房间坐标错误"));
            return;
        }

        if (location.getAddress() == null || location.getAddress().equals("")) {
            messageContext.addMessage(ValidateUtils.error("location.detailedDescription", "未获取定位信息"));

            return;
        }


        if (location.getDetailedDescription() == null || location.getDetailedDescription().length() < 2) {
            messageContext.addMessage(ValidateUtils.error("location.detailedDescription", "具体位置信息必须多于2个字符"));
            return;
        }

        if (location.getDetailedDescription().length() > 20) {
            messageContext.addMessage(ValidateUtils.error("location.detailedDescription", "具体位置信息必须少于20个字符"));
        }


    }

    public void validatePrice(Room room, ValidationContext context) {


        MessageContext messageContext = context.getMessageContext();

        List<Price> prices = room.getPrices();
        boolean flag = true;
        //校验结果必须有一个有效的价格
        for (Price price : prices) {
            if (price.getPrice() >= 0) {
                if (!Price.ALLOWED_TIME_UNITS.contains(price.getTimeUnit())) {
                    messageContext.addMessage(ValidateUtils.error("prices", "时间单位错误"));
                }

                if (price.getNum() <= 0) {
                    messageContext.addMessage(ValidateUtils.error("prices", "无效的时间数量"));
                }

                price.setText(price.toString());

                flag = false;
            }
        }

        if (flag) {
            messageContext.addMessage(ValidateUtils.error("prices", "没有有效的定价"));
        }

    }


    public void validateConfirmation(Room room, ValidationContext context) {


        //执行收尾工作

        //去除无效定价
        room.getPrices().removeIf(it -> it.getPrice() < 0);

        //定价去重
        room.setPrices(Lists.newArrayList(ImmutableSet.copyOf(room.getPrices()).asList()));

    }


}
